home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Pascal
/
Book Demos in Pascal
/
ResourcesDemo
/
ResourcesDemo.p
< prev
next >
Wrap
Text File
|
1995-05-26
|
2KB
|
78 lines
program ResourcesDemo;
(* ResourcesDemo *)
(* by Ingemar Ragnemalm 1995 *)
(* Demonstrates how to use custom resources from your programs. *)
(* A demo program that has a resource, stored in itself (should rather be in a *)
(* preference file in a real program) with a single variable, myCount. *)
(* Each time the program is run, the resource is read, myCount is incremented, and *)
(* the program beeps as many times as myCount says. *)
{$IFC UNDEFINED THINK_PASCAL}
uses Types, QuickDraw, Memory, Resources;
{$ENDC}
(* Define a structure and a handle type to it. This is our resource format! *)
type
MyRecord = record
myCount: Integer;
end;
MyPtr = ^MyRecord;
MyHnd = ^MyPtr;
(* Resource type and number. Use mixed-type names to avoid conflicts *)
const
kMyResType = 'Demo';
kMyResNum = 0;
var
myResource: MyHnd; (* The handle to our resource *)
i: Integer; (* An integer for use as loop variable *)
procedure InitToolbox;
begin
{$IFC UNDEFINED THINK_PASCAL}
InitGraf(@qd.thePort);
InitCursor;
MaxApplZone;
{$ENDC}
end; (*InitToolbox*)
begin
InitToolbox;
(**** Get the resource ****)
myResource := MyHnd(GetResource(kMyResType, kMyResNum));
(**** Create the resource if needed ****)
(* If it didn't load, we assume it doesn't exist. Create it! *)
(* This is done by allocating a handle, initializing the fields and calling AddResource. *)
if myResource = nil then
begin
myResource := MyHnd(NewHandle(sizeof(MyRecord))); (* Allocates memory *)
myResource^^.myCount := 0; (* Init fields *)
AddResource(Handle(myResource), kMyResType, kMyResNum, ''); (* Create resource *)
end;
(**** Change the resource ****)
myResource^^.myCount := myResource^^.myCount + 1; (* Change the count *)
(* Call ChangedResource to tell MacOS that the resource needs to be written *)
(* back to disk. *)
ChangedResource(Handle(myResource));
(**** Use the resource. Beep as many times as myCount says. ****)
for i := 1 to myResource^^.myCount do
SysBeep(1);
end. (* ResourcesDemo *)